<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
mm_showMyFileName(__FILE__);

// session specific variable auth (useful for show_price_including_tax, etc.)
$auth = $_SESSION['auth'];
// is tax included?
$tax_included = $auth["show_price_including_tax"];

// namespace definition
$namespaces = array(
	'base' =>  JURI::base() . $param['owl_filename'],
	'dc' => 'http://purl.org/dc/elements/1.1/',
	'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
	'xsd' => 'http://www.w3.org/2001/XMLSchema#',
	'owl' => 'http://www.w3.org/2002/07/owl#',
	'vcard' => 'http://www.w3.org/2006/vcard/ns#',
	'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
	'gr' => 'http://purl.org/goodrelations/v1#',
	'self' => JURI::base() . $param['owl_filename'] . '#'
);

$elements = array(
	//'BusinessFunction', // not needed anymore, is directly saved as list param
	'BusinessEntityType',
	'DeliveryMethod',
	'PaymentMethod',
	//'eligibleRegions', // not needed, is directly saved in textfield param called eligibleRegions
	'WarrantyScope'
);

$db = new ps_DB;
$psp = new ps_product;

// fetch all parameter names that are instances of an element (like eligibleRegions) in goodrelations
function getAllParametersFromElement($search) {
	global $db;
	
	$db->query("select params from #__modules where module='mod_semanticweb'");
	if($db->next_record()) {
		$param_array = explode("\n", $db->f('params'));
		foreach($param_array as $equation) {
			list($param_name, $param_value) = explode("=", trim($equation));
			if(eregi($search, $param_name) && $param_name != '') { // key cannot be empty
				$param_key = ereg_replace('(_|'.$search.')', '', $param_name);
				if($param_value == '0') // skip radio buttons set to zero ('No')
					continue;
				else if($param_value == '1') { // one of the radio buttons ('Yes')
						$param[$param_key] = true; // assign true
				}
				else { // a text field param
					$param[$param_key] = $param_value;
				}
			}
		}
		return $param;
	}
}

// create w3c date format from any different time/date format
function w3cDate($time, $days_offset=0) {
	$time = $time+$days_offset*86400;
	if ((int) PHP_VERSION >= 5)
		return date('c', $time);
	else {
		$offset = date('O', $time);
		return date('Y-m-d\TH:i:s', $time).substr($offset, 0, 3).':'.substr($offset, -2);
	}
}

// unit of measurement
if(sizeof(getAllParametersFromElement('uom_weight'))>0)
$uom_weight = array_pop(getAllParametersFromElement('uom_weight'));
if(sizeof(getAllParametersFromElement('uom_lwh'))>0)
$uom_lwh = array_pop(getAllParametersFromElement('uom_lwh'));

// vendor details
$db->query("select t1.vendor_id, t1.vendor_name, t1.contact_title, t1.contact_first_name,"
	. " t1.contact_middle_name, t1.contact_last_name, t1.vendor_store_name, t1.vendor_url,"
	. " t1.vendor_zip, t1.vendor_address_1, t1.vendor_city, t1.vendor_state, t1.vendor_country,"
	. " t1.contact_email, t1.vendor_phone, t1.contact_fax"
	. " from #__{vm}_vendor t1"
	. " left join #__{vm}_product t2 on t1.vendor_id=t2.vendor_id"
	. " where t2.product_id='$product_id'");
if($db->next_record()) {
	$vendor_id = intval($db->f('vendor_id'));
	$vendor_name = ($db->f('contact_title')?$db->f('contact_title')." ":"")
		. ($db->f('contact_first_name')?$db->f('contact_first_name')." ":"")
		. ($db->f('contact_middle_name')?$db->f('contact_middle_name')." ":"")
		. ($db->f('contact_last_name')?$db->f('contact_last_name'):"");
	$vendor_store_name = $db->f('vendor_store_name');
	$vendor_url = $db->f('vendor_url');
	$vendor_postal_code = $db->f('vendor_zip');
	$vendor_street_address = $db->f('vendor_address_1');
	$vendor_locality = $db->f('vendor_city');
	$vendor_region = $db->f('vendor_state');
	$vendor_country_name = $db->f('vendor_country');
	$vendor_email = $db->f('contact_email');
	$vendor_phone = $db->f('vendor_phone');
	$vendor_fax = $db->f('contact_fax');
}

// manufacturer
$db->query("select mf_name, mf_desc, mf_url from #__{vm}_manufacturer where manufacturer_id='$manufacturer_id'");
if($db->next_record()) {
	$manufacturer_name = $db->f('mf_name');
	$manufacturer_description = $db->f('mf_desc');
	$manufacturer_url = $db->f('mf_url');
}

// categories
// example: $category[0]['name'] = "Hand Tools"
$db->query("select t1.category_id, t1.category_name, t1.category_description, t2.category_parent_id"
	. " from #__{vm}_category t1"
	. " left join #__{vm}_category_xref t2 on t1.category_id=t2.category_child_id"
	. " left join #__{vm}_product_category_xref t3 on t1.category_id=t3.category_id"
	. " where t1.category_publish='Y' and t3.product_id='$product_id'");
if($db->next_record()) {
	$temp['id'] = intval($db->f('category_id'));
	$temp['name'] = $db->f('category_name');
	$temp['description'] = $db->f('category_description');
	$temp['parent_id'] = $category_parent_id = intval($db->f('category_parent_id'));
	$category[] = $temp;
	while($category_parent_id != 0) { // traverse till we get to root category (0..n, where n is root category)
		$db->query("select t1.category_id, t1.category_name, t1.category_description, t2.category_parent_id"
			. " from #__{vm}_category t1"
			. " left join #__{vm}_category_xref t2 on t1.category_id=t2.category_child_id"
			. " where t1.category_publish='Y' and t1.category_id='$category_parent_id'");
		$temp['id'] = intval($db->f('category_id'));
		$temp['name'] = $db->f('category_name');
		$temp['description'] = $db->f('category_description');
		$temp['parent_id'] = $category_parent_id = intval($db->f('category_parent_id'));
		$category[] = $temp;
	}
}

// get parameters from the settings in the backend
// example: $param['owl_filename'] = "semanticweb.owl"
/*
$db->query("select params from #__modules where module='mod_semanticweb'");
if($db->next_record()) {
	$param_array = explode("\n", $db->f('params'));
	foreach($param_array as $equation) {
		list($param_name, $param_value) = explode("=", trim($equation));
		if($param_name != '') { // key cannot be empty
			$param[$param_name] = $param_value;
		}
	}
}
*/

// product tax
$product_tax = $tax_included?$psp->get_product_taxrate($product_id):0.00;
// product price
$product_base_price = $GLOBALS['CURRENCY']->convert($product_price_raw['product_base_price'], $product_price_raw['product_currency'], $GLOBALS['product_currency']);
$product_base_price = $product_base_price*(1+$product_tax);
$product_currency = $GLOBALS['product_currency'];

// price quantity
$db->query("select price_quantity_start, price_quantity_end from #__{vm}_product_price where product_id='$product_id' and shopper_group_id='".$auth["shopper_group_id"]."'");
if($db->next_record()) {
	$price_quantity_start = $db->f('price_quantity_start');
	$price_quantity_end = $db->f('price_quantity_end');
}

// product special price
if($product_special == 'Y') {
	$db->query("select amount, is_percent, start_date, end_date from #__{vm}_product_discount where discount_id='$product_discount_id'");
	if($db->next_record()) {
		$product_special_amount = $db->f('amount');
		$product_special_is_percent = $db->f('is_percent');
		$product_special_start_date = $db->f('start_date');
		$product_special_end_date = $db->f('end_date');
		// Calculate discounted special price
		$product_special_price = $product_base_price;
		if( !empty($product_special_amount)) {
			switch( $product_special_is_percent ) {
				case 0:
					// If we subtract discounts BEFORE tax
					if( PAYMENT_DISCOUNT_BEFORE == '1' ) {
						// and if our prices are shown with tax
						if( $auth["show_price_including_tax"] == 1) {
							// then we add tax to the (untaxed) discount
							$product_special_amount += ($product_tax*$product_special_amount);
						} 
						// but if our prices are shown without tax
							// we just leave the (untaxed) discount amount as it is
							
					}
					// But, if we subtract discounts AFTER tax
						// and if our prices are shown with tax
							// we just leave the (untaxed) discount amount as it is
						// but if  prices are shown without tax
							// we just leave the (untaxed) discount amount as it is
							// even though this is not really a good combination of settings

					$product_special_price -= $product_special_amount;
					break;
				case 1:
					$product_special_price *= (100 - $product_special_amount)/100;
					break;
			}
		}
	}
}
?>